#pragma once
#include<string>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include<atomic>
#include<string>
#include<fstream>
#include<boost/algorithm/string.hpp>
#include<vector>
namespace ns_util
{


  const std::string temp_path="./temp/";
 class PathUtil{
 public:
  static std::string AddSuffix(const std::string&file_name,const std::string& suffix)
  {
     std::string path_name=temp_path;
     path_name+=file_name;
     path_name+=suffix;

 return path_name;
  }

//编译
  static std::string Src(const std::string&file_name)
  {
    return AddSuffix(file_name,".cpp");
  }

  static std::string Exe(const std::string& file_name)
  {
    return AddSuffix(file_name,".exe");
  }
  static std::string CompilerError(const std::string& file_name)
  {
    return AddSuffix(file_name,".compile_error");
  }
   


  //运行时的临时文件
  
  static std::string Stdin(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stdin");
        }
  static std::string Stdout(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stdout");
        }
  static std::string Stderr(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stderr");
        }

    
 };

class UtilTime
{
  public:
  //获取时间撮

  static std::string gettimeStamp()
  {

    struct timeval _time;
    gettimeofday(&_time,nullptr);
    return std::to_string(_time.tv_sec);
  }
    static std::string GetTimeMs()
   {
      struct timeval _time;
      gettimeofday(&_time,nullptr);
      return std::to_string(_time.tv_sec*1000+_time.tv_usec/1000);

   }



};



class FileUtil
{
public:
   static bool IsFileExists(const std::string& file_name)
   {
     struct stat st;
       if(stat(file_name.c_str(),&st)==0)
       return true;

       return false;
   }
   

   //形成唯一文件名
   static std::string UniqFileName()
   {
    static std::atomic_uint id(0);
     id++;
    std::string ms=UtilTime::GetTimeMs();
    std::string d=std::to_string(id);
    
    return ms+d;
    
   }
   //写入文件
   static bool WriteFile(const std::string &target,std::string& code)
   {
       std::ofstream out(target); 
       if(!out.is_open())
       {
        return false;
       }
       out.write(code.c_str(),code.size());
       out.close();
       return true;


   }

   static bool ReadFile(const std::string& target,std::string* content,bool keep=false)
   {
    (*content).clear();
    std::ifstream in(target);
    if(!in.is_open())
    {
      return false;
    }
    std::string line;

    while(std::getline(in,line))
    {
      
      line+=( keep ? "\n" : "");
      (*content)+=line;
    }
    //std::cout<<"line:\n\t"<<line;
    in.close();
    return true;

   }



};
 
 //时间错
 class TimeUtil
 {
  public:
  static std::string GetTimeStamp()
  {
      struct timeval time;

    return std::to_string(gettimeofday(&time,nullptr));

  }


 };


 class StringUtil
 {
 public:

 static void SplitString(const std::string& src,std::vector<std::string>* target,const std::string& sep)
 {
      boost::split((*target),src,boost::is_any_of(sep),boost::algorithm::token_compress_on);
 }


 };


}